package cn.minimelon.api.controller.module;

import cn.minimelon.api.constant.UserConstants;
import cn.minimelon.api.controller.common.BaseController;
import cn.minimelon.api.domain.common.AjaxResult;
import cn.minimelon.api.domain.module.po.DataArea;
import cn.minimelon.api.service.module.IDataAreaService;
import cn.minimelon.api.utils.MelonUtils;
import org.noear.solon.annotation.*;
import org.noear.solon.validation.annotation.HttpPart;
import org.noear.solon.validation.annotation.NoRepeatSubmit;

import java.util.Date;
import java.util.List;

/**
 * 资产主题
 *
 * @author hiro
 */
@Controller
@Mapping("/module/area")
public class DataAreaController extends BaseController {
    @Inject
    private IDataAreaService areaService;

    /**
     * 获取主题配置列表
     */
    @Get
    @Mapping("/list")
    public AjaxResult list(DataArea dataArea) {
        List<DataArea> list = areaService.selectList(dataArea);
        return AjaxResult.success(list);
    }

    /**
     * 根据主题编号获取详细信息
     */
    @Get
    @Mapping(value = "/{uid}")
    public AjaxResult getInfo(String uid) {
        return AjaxResult.success(areaService.selectById(uid));
    }

    /**
     * 新增主题配置
     */
    @NoRepeatSubmit(value={HttpPart.body})
    @Post
    @Mapping
    public AjaxResult add(DataArea dataArea) {
        if (UserConstants.NOT_UNIQUE.equals(areaService.checkCodeUnique(dataArea))) {
            return AjaxResult.error("新增主题'" + dataArea.getName() + "'失败，主题编码已存在");
        }
        dataArea.setUid(MelonUtils.snowId("A"));
        dataArea.setCreateTime(new Date());
        dataArea.setCreateBy(getUsername());
        return toAjax(areaService.insert(dataArea));
    }

    /**
     * 修改主题配置
     */
    @NoRepeatSubmit(value={HttpPart.body})
    @Put
    @Mapping
    public AjaxResult update(DataArea dataArea) {
        if (UserConstants.NOT_UNIQUE.equals(areaService.checkCodeUnique(dataArea))) {
            return AjaxResult.error("修改主题'" + dataArea.getName() + "'失败，主题编码已存在");
        }
        dataArea.setUpdateBy(getUsername());
        dataArea.setUpdateTime(new Date());
        return toAjax(areaService.update(dataArea));
    }

    /**
     * 删除主题配置
     */
    @Delete
    @Mapping("/{uid}")
    public AjaxResult remove(String uid) {
        // 校验是否存在模型
        areaService.deleteById(uid);
        return success();
    }
}
